/*
 * 作者:熊猫
 * 日期:2017 - 3 - 20 10 - 10 - 6
 *
 */

package net.lainiao.morethread.Chapter8.demo1;

import java.util.Collection;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Administrator on 2017/3/20.
 */
public class MyLock extends ReentrantLock {
    public String getOwnerName() {
        if (this.getOwner() == null) {
            return "None";
        }
        return this.getOwner().getName();
    }

    public Collection<Thread> getThreads() {
        return this.getQueuedThreads();
    }

    public class Task implements Runnable {

        protected Lock lock;

        public Task(Lock lock){
            this.lock=lock;
        }

        @Override
        public void run() {
            for (int i = 0; i < 5; i++) {
                lock.lock();
                System.out.println(Thread.currentThread().getName()+":Get the Lock");
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                    System.out.println(Thread.currentThread().getName()+" : Free The Lock");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
